import java.sql.PreparedStatement;
import java.util.ArrayList;

//SteveM has
//Commented out all lines with dispH

public class Architect {// Must have access to display ovject in order to update the overlay    
   public static void validateSelectedBlock(XMLDTO vXMLDTO){


        //Changes by Jonathan to include display
        /* All authentication is stored in a file credential.clf
        * The file is structured to include the following:
        * googleloginname@gmail.com
        * passwordforgmailac
        */
        //Typically use the buildandcontrolapi@gmail.com login
        String cFile="credential.clf";        
        //Encrypted Table ID for Build and Control Display Fusion Table
        String tableID_enc="1Fa37L3pF1mEsNObjf6BRBbnAgP8_6HUmEyPkXCU";

        //TeamDisplayHandler dispH = new TeamDisplayHandler(tableID_enc);
		//boolean authenticatedToGoogle = dispH.authenticate(cFile);
		boolean valueChanged;

	       DatabaseHelper vDatabaseHelper = new DatabaseHelper();
	       Architect vArchitect = new Architect();
	       ArrayList vArrayList = new ArrayList();
	       int qRow = vXMLDTO.getRow();
	       int qColumn = vXMLDTO.getCol();
	       //System.out.println(qRow + "   " + qColumn);
	       
	       //valueChanged=dispH.blockSetTeam(3, 30, "Red - Activated");
       //System.out.println("GOT HERE");
       
       if (vArchitect.isNotResource(vXMLDTO, vDatabaseHelper)){           
           if (vArchitect.didNotExist(vXMLDTO, vDatabaseHelper)){
               vDatabaseHelper.activate_block(vXMLDTO);
               if (vXMLDTO.getTeam().equals("Red")){
                   //update overlay qRow, qCol, 101 (only red active)
                  //Typical output for red Activated
                  //valueChanged=dispH.blockSetTeam(qRow, qColumn, "Red - Activated");
               }else{
                   //update overlay qRow, qCol, 11 (only blue active)
                  //valueChanged=dispH.blockSetTeam(qRow, qColumn, "Blue - Activated");
               }
               //update overlay
           }else {
               if (vArchitect.isNotOwned(vXMLDTO, vDatabaseHelper)){
                   vDatabaseHelper.reactivate_block(vXMLDTO);

                   vArrayList = vDatabaseHelper.returnLandClaimDTO(vXMLDTO);
                   
                   String OwningTeam = null;
                   String RequestingTeam = null;
                   boolean Blue_Active = false;
                   boolean Red_Active = false;
                   
                   RequestingTeam = vXMLDTO.getTeam();
                   OwningTeam = ((LandClaimDTO) vArrayList.get(0)).getTeamName();
                   Blue_Active = ((LandClaimDTO) vArrayList.get(0)).isBlueActive();
                   Red_Active = ((LandClaimDTO) vArrayList.get(0)).isRedActive();
                   final String defaultTeam = "Default";
                   final String redTeam = "Red";
                   //final String blueTeam = "Blue";

                   if (OwningTeam.equals(defaultTeam)){//if neither team owns it
                       if ((Blue_Active) && (Red_Active)){
                           //update overlay qRow, qCol, 11000 (competed)
                        //valueChanged=dispH.blockSetTeam(qRow, qColumn, "Competed - Blue/Red");
                       }else if (Red_Active){
                           //update overlay qRow, qCol, 101 (only red active)
                          //valueChanged=dispH.blockSetTeam(qRow, qColumn, "Red - Activated");
                       }else{ // if only activated by blue
                           //update overlay qRow, qCol, 11 (only blue active)
                        //valueChanged=dispH.blockSetTeam(qRow, qColumn, "Blue - Activated");
                       }
                   }else if (OwningTeam.equals(redTeam)){
                       if (Blue_Active){
                           //update overlay qRow, qCol, 102 (Red targeted by blue)
                        //valueChanged=dispH.blockSetTeam(qRow, qColumn, "Red - Targeted by Blue");

                       }
                   }else{//if owned by blue
                       if (Red_Active){
                           //update overlay qRow, qCol, 12 (Blue targetted by red)
                        //valueChanged=dispH.blockSetTeam(qRow, qColumn, "Blue Target by Red");
                       }
                   }                   
                   //update overlay
               }else{
                   // give an error saying ' your team owns land already (possibly through display object
               }  
           }
       }else{
           //give error saying block is a resource and cant be activated
       }
   }
   
   public boolean isNotResource(XMLDTO vXMLDTO, DatabaseHelper vDatabaseHelper){// Check if the block being activated is a resource block
       ArrayList vArrayList = new ArrayList();
       vArrayList = vDatabaseHelper.returnResourceDTO(vXMLDTO);
       if (vArrayList.isEmpty()){
           return true; 
       }else {
           return false; 
       }
   }
   
   public boolean isNotOwned(XMLDTO vXMLDTO, DatabaseHelper vDatabaseHelper){// Check if activating team already owns the block
       ArrayList vArrayList = new ArrayList();
       vArrayList = vDatabaseHelper.returnLandClaimDTO(vXMLDTO);
       String vTeam_Name = null;
       vTeam_Name = (((LandClaimDTO) vArrayList.get(0)).getTeamName());
       if (vTeam_Name.equals(vXMLDTO.getTeam())){
           return false; 
       }else {
           return true; 
       }
   }
   
   public boolean didNotExist(XMLDTO vXMLDTO, DatabaseHelper vDatabaseHelper){// Check if the block is already nthe land claim table, if not then insert, if is then update
       ArrayList vArrayList = new ArrayList();
       vArrayList = vDatabaseHelper.returnLandClaimDTO(vXMLDTO);
       if (vArrayList.isEmpty()){
           return true;
       }else {
           return false; 
       }
   }
}
